﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jy123.Model;
using Jy123.BLL;
using Jy123.BLL.UI;
using Jy123.BLL.User;
using Jy123.Common;
using Jy123.Common.Config;

namespace Jy123.Web
{
    public partial class Login : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie SNSToKenCookie = Context.Request.Cookies["SNSUserPassPort"];//获取客户端COOKIE
            if (SNSToKenCookie != null)//当cookie不为空
            {
                SNSToKenCookie.Values.Clear();
                SNSToKenCookie.Domain = Jy123.Common.Config.UiConfig.CookieDomain;
                SNSToKenCookie.Expires = DateTime.Now.AddDays(20);
                Response.SetCookie(SNSToKenCookie);
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (Request.QueryString["at"] != null)
                {
                    string at = Request.QueryString["at"].ToString();
                    if (at == "Submit")
                    {
                        Submit();
                    }
                }

            }
            
            
        }

        //当点击登陆提交按钮
        public void Submit()
        {

            string email = Request.Form["TxtLogEmail"].Trim();
            string Password = Request.Form["TxtLogPassword"].Trim();
            string IsAuto = Request.Form["ChbLogAuto"];
            string urls = Request.Form["Retrunurls"];
            Jy123.BLL.User.UserSql usSql = new UserSql();
            int userId = 0;
            string userName = string.Empty;
            string userPortrait = string.Empty;
            int loginNum = 0;

            //登陆状态
            EnumLoginState state = usSql.Login(email, Password, out userId, out userName, out userPortrait, out loginNum);
            Model.User_Main modelUser = new Jy123.Model.User_Main();
            //BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            modelUser.ID = userId;
            modelUser.Email = email;

            modelUser.UserName = userName;
            
            switch (state)
            {
                case EnumLoginState.Succeed:
                    if (IsAuto == "on")
                    {
                        Jy123.Common.Cookie.SetCookie("SNSUserPassPort", modelUser.ToString(),20);    //设置COOKIE
                    }
                    
                    //if (string.IsNullOrEmpty(userPortrait) && loginNum == 0)
                    //{
                        Response.Redirect("Default.aspx");
                    //}
                    break;
                case EnumLoginState.Err_UnActivation:
                    //PageError("帐号(" + email + ")未通过<a href=\"EmailActive.aspx\">电子邮件激活</a>", email);
                    break;
            }
        }
    }
}
